package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.SecurityTrainingBean;
import com.sp.scmdb.bean.SecurityTrainingTypeBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.SecurityTrainingTypeDao;
import com.sp.scmdb.entity.SecurityTraining;
import com.sp.scmdb.entity.SecurityTrainingType;
import com.sp.scmdb.service.SecurityTrainingTypeService;

@Service("securityTrainingTypeService")
public class SecurityTrainingTypeServiceImpl implements SecurityTrainingTypeService {

	private SecurityTrainingTypeDao securityTrainingTypeDao;
	
	public void setSecurityTrainingTypeDao(SecurityTrainingTypeDao securityTrainingTypeDao) {
		this.securityTrainingTypeDao = securityTrainingTypeDao;
	}

	@Override
	@Transactional
	public String createSecurityTrainingType(SecurityTrainingTypeBean bean) {
		String result = "2";
		SecurityTrainingType trainingType = new SecurityTrainingType();
		trainingType.setTrainTypeName(bean.getTrainTypeName());
		trainingType.setUsed(true);
		
		securityTrainingTypeDao.save(trainingType);
		result = CommonParamter.CHECK_SUCCESS;
		
		return result;
	}

	@Override
	@Transactional
	public String delSecurityTrainingType(String[] arr) {
		String result = "2";
		SecurityTrainingType securityTrainingType = null;
		for (int i = 0; i < arr.length; i++) {
			securityTrainingType = securityTrainingTypeDao.getBySysId(arr[i]);
			securityTrainingTypeDao.delete(securityTrainingType);
		}
		result = CommonParamter.CHECK_SUCCESS;
		return result;
	}

	@Override
	@Transactional
	public String editSecurityTrainingType(SecurityTrainingTypeBean bean) {
		String result = "2";
		SecurityTrainingType trainingType = null;
		trainingType = securityTrainingTypeDao.getBySysId(bean.getSysId());
		trainingType.setTrainTypeName(bean.getTrainTypeName());
		
		securityTrainingTypeDao.update(trainingType);
		result = CommonParamter.CHECK_SUCCESS;
		
		return result;
	}

	@Override
	public List<SecurityTrainingTypeBean> getListSecurityTrainingType(String hql) {
		SecurityTrainingTypeBean securityTrainingTypeBean = null;
		SecurityTrainingType securityTrainingType = null;
		String _hql = "from SecurityTrainingType where 1 = 1 and Used = 1";
		List<SecurityTrainingType> list = securityTrainingTypeDao.queryHql(_hql);
		List<SecurityTrainingTypeBean> securityTrainingTypeList = new ArrayList<SecurityTrainingTypeBean>();
		
		try {
			for(int i = 0; i < list.size(); i ++) {
				securityTrainingType = list.get(i);				
				securityTrainingTypeBean = new SecurityTrainingTypeBean();
				securityTrainingTypeBean.setSysId(securityTrainingType.getSysId());
				securityTrainingTypeBean.setTrainTypeName(securityTrainingType.getTrainTypeName());
				securityTrainingTypeBean.setUsed(securityTrainingType.getUsed());
				
				securityTrainingTypeList.add(securityTrainingTypeBean);
			}
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return securityTrainingTypeList;
	}

	@Override
	public SecurityTrainingTypeBean getSecurityTrainingType(String sysId) {
		SecurityTrainingType securityTrainingType = securityTrainingTypeDao.getBySysId(sysId);
		SecurityTrainingTypeBean securityTrainingTypeBean = null;
		try {
			if(securityTrainingType != null) {
				securityTrainingTypeBean = new SecurityTrainingTypeBean();
				securityTrainingTypeBean.setSysId(securityTrainingType.getSysId());
				securityTrainingTypeBean.setTrainTypeName(securityTrainingType.getTrainTypeName());
				securityTrainingTypeBean.setUsed(securityTrainingType.getUsed());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return securityTrainingTypeBean;
	}

}
